package com.geoboard.service;

import java.util.ArrayList;

import com.geoboard.R;
import com.geoboard.application.GeoBoardApplication;
import com.geoboard.entity.Post;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ListAdapterPosts extends BaseAdapter{
	
	/**** Members *******************************************/
	private ArrayList<Post> data = null;
	private Context context = null;
	
	/**** Constructors **************************************/
	public ListAdapterPosts(ArrayList<Post> data, Context c)throws IllegalArgumentException{
		if(data == null || c == null){
			throw new IllegalArgumentException();
		}
		
		this.data = data;
		this.context = c;
	}
	
	/**** Base Adapter Overrides *****************************/

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.size();
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
        if(v == null){
            LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.list_adap_post_row, null);
         }
        
        TextView text = (TextView)v.findViewById(R.id.post_text);
        TextView user = (TextView)v.findViewById(R.id.post_username);
        TextView date = (TextView)v.findViewById(R.id.post_date);
        Post p = data.get(position);

        text.setText(p.getText());
        user.setText(p.getUserName());
        date.setText(p.getTimestamp().toString());
        
        //highly complex security
        if (p.getUserName().equals(GeoBoardApplication.getUser().getUserName()));
		return v;
	}
}
